﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class TreatmentProgressDisplayCommand extends Command {

		[Inject]
        public var event:InfoEvent;
		[Inject]
        public var treatmentProgressModel:TreatmentProgressDisplayModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;

        private static const NO_TREATMENT_RETURNED_TO_OBSERVE:TreatmentVO = null;

        override public function execute():void{
            var _local1:String = this.event.id;
            switch (_local1){
                case EventConstants.REQUEST_FOR_FIRST_TO_FINISH_TREATMENT:
                    this.requestForNewFirstToFinishTreatment();
                    break;
                case EventConstants.ON_TREATMENT_STARTED:
                    this.onTreatmentStarted();
                    break;
                case EventConstants.ON_TREATMENT_FINISHED:
                    this.onTreatmentFinished();
                    break;
                case EventConstants.ON_TREATMENT_FINALIZED:
                    this.onTreatmentFinalized();
                    break;
                default:
                    throw (new Error("There is no updateIdentifier (event.id) set in the InfoEvent. Check TreatmentController command"));
            };
        }
        private function requestForNewFirstToFinishTreatment():void{
            var _local1:TreatmentVO = this.getFirstToFinishFromAllTreatments();
            if (_local1 == NO_TREATMENT_RETURNED_TO_OBSERVE){
                this.treatmentProgressModel.noTreatmentToMonitor();
            } else {
                this.treatmentProgressModel.treatmentHasStarted(_local1);
            };
        }
        private function onTreatmentStarted():void{
            var _local1:TreatmentVO = this.getFirstToFinishFromAllTreatments();
            if (_local1 == NO_TREATMENT_RETURNED_TO_OBSERVE){
                this.requestForNewFirstToFinishTreatment();
            };
            this.treatmentProgressModel.treatmentHasStarted(_local1);
        }
        private function onTreatmentFinished():void{
            this.treatmentProgressModel.treatmentThatIsFinished(this.verifyTreatmentVO());
        }
        private function onTreatmentFinalized():void{
            this.treatmentProgressModel.treatmentThatIsFinalized(this.verifyTreatmentVO());
        }
        private function verifyTreatmentVO():TreatmentVO{
            var vo:TreatmentVO;
            try {
                vo = TreatmentVO(this.event.info);
            } catch(err:TypeError) {
                throw (new Error(("Those damn untyped Objects!!!" + err.message)));
            };
            return (vo);
        }
        private function getFirstToFinishFromAllTreatments():TreatmentVO{
            var _local1:Boolean;
            var _local2:Array = this.worldModel.getAllRunningTreatments(_local1);
            var _local3:TreatmentVO = _local2[0];
            return (((_local3) || (NO_TREATMENT_RETURNED_TO_OBSERVE)));
        }

    }
}//package net.wooga.pets.controllers 
